$import( "SmitearWebGL.Events.EventDispatcher" );
$import( "SmitearWebGL.Events.TimerEvent" );

SmitearWebGL.Utils.Timer = SmitearWebGL.Events.EventDispatcher.extend(
{
	/******** constructor ********/

	init : function( frequency, repeatCount )
	{
		/******** private property ********/

		this._running = false;
		this._count = 0;
		this._repeatCount = repeatCount == undefined ? 0 : repeatCount;
		this._frequency = frequency == undefined ? 0 : frequency;
		this._timer = null;
		this._timerEvent = new SmitearWebGL.Events.TimerEvent( SmitearWebGL.Events.TimerEvent.TIMER , this._frequency );
		
		this._super();
		this._registClass( "SmitearWebGL.Utils.Timer" );
	}
	,

	/******** public method ********/

	start : function()
	{
		if ( this.hasEventListener( SmitearWebGL.Events.TimerEvent.TIMER ) )
		{
			var self = this;
			this._timer = setInterval( function(){ self._do() }, this._frequency );
			
			this._running = true;
		}
		else
			$WARNING( "no listener" );
	}
	,
	stop : function()
	{
		clearInterval( this._do );
		this._count = 0;
		this._timerEvent.count = 0;
		this._running = false;
	}
	,
	isRunning : function()
	{
		return this._running;
	}
	,

	/******** private method ********/

	_do : function()
	{
		this._timerEvent.count = this._count;
		
		this.dispatchEvent( this._timerEvent );
		this._count ++ ;
		
		if ( this._count > this._repeatCount )
			this.stop();
	}
}
);